package com.gopay.common.cps.manager.gsrt.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.gopay.common.constants.gsrt.BidConstants;
import org.springframework.stereotype.Service;

import com.gopay.common.cps.dao.gsrt.GsrtCustTenderMasterDao;
import com.gopay.common.cps.manager.gsrt.GsrtCustTenderMasterQueryManager;
import com.gopay.common.domain.gsrt.GsrtCustTenderMaster;

@Service(value = "gsrtCustTenderMasterQueryManager")
public class GsrtCustTenderMasterQueryManagerImpl implements GsrtCustTenderMasterQueryManager {
	
	@Resource(name = "gsrtCustTenderMasterDao")
	public GsrtCustTenderMasterDao gsrtCustTenderMasterDao;

	@Override
	public GsrtCustTenderMaster getByGopayOrderId(String gopayOrderId) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		//paramMap.put("merCustId", merCustId);
		paramMap.put("gopayOrderId", gopayOrderId);
		String hql = "from GsrtCustTenderMaster where gopayOrderId=:gopayOrderId";
		return (GsrtCustTenderMaster) (gsrtCustTenderMasterDao.findByHql(hql, paramMap).size() > 0
				? gsrtCustTenderMasterDao.findByHql(hql, paramMap).get(0) : null);
	}
	/**
	 * 插入信息
	 */
	@Override
	public GsrtCustTenderMaster save(GsrtCustTenderMaster master)   {
		return gsrtCustTenderMasterDao.save(master);

	}
	/**
	 * 更細信息
	 */
	@Override
	public void update(GsrtCustTenderMaster master)   {
		 gsrtCustTenderMasterDao.update(master);
		
	}

	/**
	 * 查询标的信息
	 * 
	 * 
	 * @param merCustId
	 * @param bidNo
	 * @return
	 * @ 
	 */
	@Override
	public GsrtCustTenderMaster getByIdbidNo(String merCustId, String bidNo)   {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("merCustId", merCustId);
		paramMap.put("bidNo", bidNo);
		String hql = "from GsrtCustTenderMaster where  merCustId=:merCustId and bidNo=:bidNo ";
		return (GsrtCustTenderMaster) (gsrtCustTenderMasterDao.findByHql(hql, paramMap).size() > 0
				? gsrtCustTenderMasterDao.findByHql(hql, paramMap).get(0) : null);
	}
	/**
	 * 查询标的信息
	 * MerOrderId  原商户订单号
	 * 
	 * @param merOrderId
	 * @param bidNo
	 * @return
	 * @ 
	 */
	@Override
	public GsrtCustTenderMaster getByIdbidNoOrgMerOrderId(String merCustId, String bidNo,String merOrderId)   {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("merCustId", merCustId);
		paramMap.put("bidNo", bidNo);
		paramMap.put("merOrderId", merOrderId);
		String hql = "from GsrtCustTenderMaster where  merCustId=:merCustId and bidNo=:bidNo  and merOrderId=:merOrderId";
		return (GsrtCustTenderMaster) (gsrtCustTenderMasterDao.findByHql(hql, paramMap).size() > 0
				? gsrtCustTenderMasterDao.findByHql(hql, paramMap).get(0) : null);
	}
	/**
	 * 
	 */
	@Override
	public GsrtCustTenderMaster getByIdbidNo(String bidNo) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		//paramMap.put("merCustId", merCustId);
		paramMap.put("bidNo", bidNo);
		String hql = "from GsrtCustTenderMaster where bidNo=:bidNo";
		return (GsrtCustTenderMaster) (gsrtCustTenderMasterDao.findByHql(hql, paramMap).size() > 0
				? gsrtCustTenderMasterDao.findByHql(hql, paramMap).get(0) : null);
	}
	/**
	 * 
	 * @param date	
	 * @return 生成主订单号
	 */
	@Override
    public String getGopayOrderId_gsrt(String date) {
		return gsrtCustTenderMasterDao.getGopayOrderId_gsrt(date);
	}

	@Override
	public GsrtCustTenderMaster getById_merOrder_gsrtCustTenderMaster(String merCustId, String merOrderId)
			  {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("ID", merCustId);
		paramMap.put("MER_ORDER_ID", merOrderId);
		String hql = "from GsrtCustBid where ID=:ID and MER_ORDER_ID=:MER_ORDER_ID";
		return (GsrtCustTenderMaster) (gsrtCustTenderMasterDao.findByHql(hql, paramMap).size() > 0
				? gsrtCustTenderMasterDao.findByHql(hql, paramMap).get(0) : null);
	}

	@Override
	public GsrtCustTenderMaster getByBidNoAndStatus(String merCustId, String bidNo, String bankState) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("merCustId", merCustId);
		paramMap.put("bidNo", bidNo);
		paramMap.put("bidState", BidConstants.HF_BID_STATE_03);
		paramMap.put("bankState", bankState);
		String hql = "from GsrtCustTenderMaster where merCustId=:merCustId and bidNo=:bidNo " +
				"and bidState=:bidState and bankState=:bankState";
		return (GsrtCustTenderMaster) (gsrtCustTenderMasterDao.findByHql(hql, paramMap).size() > 0
				? gsrtCustTenderMasterDao.findByHql(hql, paramMap).get(0) : null);
	}

	/**
	 * 通过创建时间查询标的信息
	 * @param date
	 * @return
	 */
	@Override
	public List<GsrtCustTenderMaster> getByCreatedTs(String date) {

		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("createdTs", date);
		paramMap.put("bidState", BidConstants.HF_BID_STATE_03);
		paramMap.put("bankState", BidConstants.HF_BANK_STATE_S);

		String hql = "from GsrtCustTenderMaster where to_char(createdTs,'YYYYMMDD') =:createdTs " +
				"and bidState=:bidState and bankState=:bankState";
		return gsrtCustTenderMasterDao.findByHql(hql, paramMap).size() > 0
				? gsrtCustTenderMasterDao.findByHql(hql, paramMap) : null;
	}
	
	@Override
    public GsrtCustTenderMaster getByMerCustIdAndBidNo(String merCustId, String bidNo)   {
        return gsrtCustTenderMasterDao.findByMerCustIdAndBidNo(merCustId, bidNo);
    }
}
